package com.goldplusgold.ui.lib.keyboard;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;

/**
 * Created by xuanweijian on 2016/3/31.
 */
public class KeyboardTouchListener implements View.OnTouchListener {
    private KeyboardUtil keyboardUtil;
    private int keyboardType = 1;
    private int scrollTo = -1;
    public static final int TYPE_TOP = 0x01;
    public static final int TYPE_ONE_THOUSAND = 0x02;
    public static final int TYPE_ONE_HUNDRED = 0x03;
    private int type = -1;
    private ScrollView scrollView;

    public KeyboardTouchListener(KeyboardUtil util, int keyboardType, int type) {
        this.keyboardUtil = util;
        this.keyboardType = keyboardType;
        this.type = type;
    }

    public KeyboardTouchListener(KeyboardUtil util, int keyboardType, int type, ScrollView scrollView) {
        this.keyboardUtil = util;
        this.keyboardType = keyboardType;
        this.type = type;
        this.scrollView = scrollView;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (type) {
            case TYPE_TOP:
                keyboardUtil.showTopLayout();
                break;
            case TYPE_ONE_THOUSAND:
                keyboardUtil.showOneThousand();
                break;
            case TYPE_ONE_HUNDRED:
                keyboardUtil.showOneHundred();
                break;
        }
        this.keyboardUtil.setScrollView(scrollView);

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (keyboardUtil != null && keyboardUtil.getEd() != null && v.getId() != keyboardUtil.getEd().getId())
                keyboardUtil.showKeyBoardLayout((EditText) v, keyboardType, scrollTo);
            else if (keyboardUtil != null && keyboardUtil.getEd() == null) {
                keyboardUtil.showKeyBoardLayout((EditText) v, keyboardType, scrollTo);
            } else {
                Log.e("KeyboardTouchListener", "v.getId():" + v.getId());
                Log.e("KeyboardTouchListener", "keyboardUtil.getEd().getId():" + keyboardUtil.getEd().getId());
                if (keyboardUtil != null) {
                    keyboardUtil.setKeyBoardCursorNew((EditText) v);
                }
            }
        }
        return false;
    }
}
